/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/


/**
 * @brief Implement a generic collection that store pairs of "tree_node, data".
 *
 * @author  WXQ#XDU
 * @date    2024.11
 */

#include "../include/amcc_hashmap.h"
#include "../include/tree.h"


static t_hashcode treemap_hash_(t_map_key K) {
    return (t_hashcode)(((ptrdiff_t)K) & 0xFFFFFFFF);
}

static int treemap_keyCmp_(t_map_key K1, t_map_key K2) {
    ptrdiff_t  delta = (ptrdiff_t)K1 - (ptrdiff_t)K2;
    if ( delta > 0 ) return 1;
    if ( delta < 0 ) return -1;
    return 0;
}


t_treemap_ptr treemap_new ( unsigned capacity_init,
                            t_mapfunc_free fpFree)
{
    t_hashmap_ptr mapPtr = hashmap_new ( capacity_init,
                                         treemap_hash_,
                                         treemap_keyCmp_,
                                         fpFree);
    return mapPtr;
}

void  treemap_destroy(t_treemap_ptr mapPtr) {
    hashmap_destroy(mapPtr);
}

int  treemap_size(t_treemap_ptr mapPtr) {
    return hashmap_size(mapPtr);
}

t_treemap_value treemap_get(t_treemap_ptr mapPtr, t_tree_ptr node) {
    return hashmap_find(mapPtr, ((t_map_key)node));
}

void  treemap_put ( t_treemap_ptr mapPtr,
                    t_tree_ptr node ,
                    t_treemap_value attribute )
{
    hashmap_put( mapPtr, (t_map_key)(node), (t_map_value)attribute );
}

void treemap_remove ( t_treemap_ptr mapPtr, t_tree_ptr node ) {
    hashmap_remove (mapPtr, (t_map_key)node);
}
